/**
 * @file displaytabctrl.h
 * @brief
 *
 *
 *
 * @author 1823419256@qq.com
 * @version 1.0
 * @date 2011
*/

#ifndef MYBOOK_DISPLAY_DISPLAYTABCTRL_H_
#define MYBOOK_DISPLAY_DISPLAYTABCTRL_H_

#include "displaymediator.h"
#include "ctrls\imageservices.h"
#include "frame\childframe.h"

enum DisplayTabStatus
{
	DTS_MOUSENORMAL	= 0,
	DTS_MOUSEOVER	= 1,
	DTS_MOUSEDOWN	= 2,
	DTS_DISABLED	= 3,
};

enum DisplayMousePosition
{
	DMP_NOCLIENT,
	DMP_ARROWLEFT,
	DMP_ARROWRIGHT,
	DMP_TABITEM,
	DMP_TABITEMCLOSE,
};

#define WM_CUSTOM_ARROWCLICK	(WM_USER + 1)
#define WM_CUSTOM_TABCLOSE		(WM_USER + 2)

class CDisplayTabCtrl;

class CTabItem
{
public:
	CTabItem(CDisplayTabCtrl* pTabCtrl);

	enum { TABITEM_WIDTH = 123, TABITEM_HEIGHT = 29 };

public:
	void DoPaint(CDCHandle dc, CMyImage* pImage, CMyImage* pImageClose, HFONT hFont);

public:		
	void SetText(LPCTSTR lpszText);
	LPCTSTR GetText() const;

	void SetUrl(LPCTSTR lpszUrl);
	LPCTSTR GetUrl() const;

	void SetChildFrame(CChildFrame* pChildFrame);
	CChildFrame* GetChildFrame() const;
		
	void SetNoClose(BOOL bNoClose);

public:
	void SetRect(const CRect& rc);
	void GetRect(CRect& rc) const;

	int GetWidth() const;
	int GetHeight() const;

public:
	bool MouseMoveEvent(const CPoint& pt, bool bInCanvas);
	bool MouseLeaveEvent();
	bool LButtomDownEvent(const CPoint& pt, bool bInCanvas, CTabItem*& pSelectItem);
	bool LButtonUpEvent(const CPoint& pt, bool bInCanvas);

	bool IsChecked() const;
	void CheckTab(bool bCheck);

protected:
	CDisplayTabCtrl* m_pTabCtrl;
	CChildFrame* m_pChildFrame;

	CString m_strUrl;
	CString m_strText;
	BOOL m_bNoClose;
	DisplayTabStatus m_eStatus;
	DisplayTabStatus m_eCloseStatus;

	CRect m_rcItem;
	CRect m_rcItemClose;
};

class CTabArrow
{
public:
	CTabArrow(CDisplayTabCtrl* pTabCtrl, bool bLeft);

	enum { TABARROW_WIDTH = 23, TABARROW_HEIGHT = 23 };

public:
	void DoPaint(CDCHandle dc, LPRECT lpRect, CMyImage* pImage);

public:
	void SetRect(const CRect& rc);
	void GetRect(CRect& rc) const;

	int GetWidth() const;
	int GetHeight() const;

public:
	bool MouseMoveEvent(const CPoint& pt, bool bInCanvas);
	bool MouseLeaveEvent();
	bool LButtomDownEvent(const CPoint& pt, bool bInCanvas);
	bool LButtonUpEvent(const CPoint& pt, bool bInCanvas);

	bool EnableArrow(bool bEnable);
	bool IsEnabled() const;

protected:
	CDisplayTabCtrl* m_pTabCtrl;

	bool m_bLeftArrow;
	DisplayTabStatus m_eStatus;
	CRect m_rcArrow;
};

class CDisplayTabCtrl
	: public CWindowImpl<CDisplayTabCtrl, CWindow, CControlWinTraits>
	, public CDoubleBufferImpl<CDisplayTabCtrl>
{
	friend class CTabItem;
	friend class CTabArrow;

public:
	DECLARE_WND_CLASS(_T("MyDisplayTabCtrl"));

	CDisplayTabCtrl();

public:
	BEGIN_MSG_MAP(CDisplayTabCtrl)
		CHAIN_MSG_MAP(CDoubleBufferImpl<CDisplayTabCtrl>)
		MESSAGE_HANDLER(WM_CREATE, OnCreate)
		MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
		MESSAGE_HANDLER(WM_SIZE, OnSize)
		MESSAGE_HANDLER(WM_MOUSEMOVE, OnMouseMove)
		MESSAGE_HANDLER(WM_MOUSELEAVE, OnMouseLeave)
		MESSAGE_HANDLER(WM_LBUTTONDOWN, OnLButtonDown)
		MESSAGE_HANDLER(WM_LBUTTONUP, OnLButtonUp)
		MESSAGE_HANDLER(WM_CUSTOM_ARROWCLICK, OnCustomArrowClick)
		MESSAGE_HANDLER(WM_CUSTOM_TABCLOSE, OnCustomTabClose)
	END_MSG_MAP()

// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

	LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnDestroy(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/);
	LRESULT OnMouseMove(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/);
	LRESULT OnMouseLeave(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnLButtonDown(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/);
	LRESULT OnLButtonUp(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/);
	
	void DoPaint(CDCHandle dc);

	LRESULT OnCustomArrowClick(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/);
	LRESULT OnCustomTabClose(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/);

public:
	void CalcCanvas();
	void RedrawArrow();
	bool EnsureVisible(CTabItem* pItem, bool bForceDraw = false);
	void ScrollTabItem(bool bLeft);
	bool RecalcTabItemPos();
	void AddTab(LPCTSTR lpszText, LPCTSTR lpszUrl, CChildFrame* pChildFrame, bool bSelectItem = false);
	void SetTabTitle(LPCTSTR lpszText, CChildFrame* pChildFrame);

public:
	void SetMediator(CDisplayMediator* pMediator)
	{
		m_pMediator = pMediator;
	}

	void ChangedAddTab(CChildFrame* pChildFrame)
	{

	}

	void ChangedDelTab(CChildFrame* pChildFrame)
	{

	}

protected:
	CDisplayMediator* m_pMediator;

protected:
	CFont m_fontText;
	CFont m_fontBoldText;
	
	std::vector<CTabItem *> m_vecTabItem;
	CTabItem* m_pSelectItem;

	CTabArrow m_ArrowLeft;
	CTabArrow m_ArrowRight;

	CMyImage m_imgTabItem;
	CMyImage m_imgTabClose;
	CMyImage m_imgTabArrowLeft;
	CMyImage m_imgTabArrowRight;

	CRect m_rcTabItemCanvas;
	CRect m_rcArrowLeftCanvas;
	CRect m_rcArrowRightCanvas;

	int m_nTabItemOffset;
	DisplayMousePosition m_eMousePos;
	bool m_bMouseLeaveEvent;
};

#endif // MYBOOK_DISPLAY_DISPLAYTABCTRL_H_